package com.xiaoyg;

import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.parsing.XPathParser;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.IOException;

/**
 * @author xiaoyg
 * @Title: XPathTest
 * @ProjectName mybatis-insider
 * @Description: TODO
 * @date 2019/4/1114:29
 */
public class XPathTest {

    public static void main(String[] args) throws ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        EntityResolver entityResolver = new XMLMapperEntityResolver();
        //开启认证
        documentBuilderFactory.setValidating(true);
        documentBuilderFactory.setNamespaceAware(false);
        documentBuilderFactory.setIgnoringComments(true);
        documentBuilderFactory.setIgnoringElementContentWhitespace(false);
        documentBuilderFactory.setCoalescing(false);
        documentBuilderFactory.setExpandEntityReferences(true);

        //创建DocumentBuilder
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();

        builder.setErrorHandler(new ErrorHandler() {
            public void warning(SAXParseException exception) throws SAXException {

                System.out.println("WARN:"+exception.getMessage());
            }

            public void error(SAXParseException exception) throws SAXException {
                System.out.println("error:"+exception.getMessage());
            }

            public void fatalError(SAXParseException exception) throws SAXException {
                System.out.println("fatalError:"+exception.getMessage());
            }
        });

        //将文档加载到一个Document对象中
        try {
            Document document = builder.parse("src/main/resources/inventory.xml");

            //创建XPathFactory
            XPathFactory factory = XPathFactory.newInstance();
            //创建XPath对象
            XPath xPath = factory.newXPath();
            //编译XPath表达式
            try {
                XPathExpression expr = xPath.compile("//book[author='Neal Stephenson']/title/text()");

                //通过XPath表达式得到结果，第一个参数指定了XPath表达式进行查询的上线文节点，也就是在指定节点下
                //查找符合XPath的节点，本例中的上下文节点是整个文档；第二个参数指定了XPath表达式的返回类型

                Object result = expr.evaluate(document, XPathConstants.NODESET);
                System.out.println("查询作者为：Neal Stephenson的图书标题：");
                NodeList nodes = (NodeList) result;
                for (int i = 0; i < nodes.getLength(); i++) {
                    System.out.println(nodes.item(i).getNodeValue());
                }

                System.out.println("查询1997年之后的图书的标题");
                nodes = (NodeList) xPath.evaluate("//boob[@year>1997]/title/text()",document,XPathConstants.NODESET);

                for (int i = 0; i < nodes.getLength(); i++) {
                    System.out.println(nodes.item(i).getNodeValue());
                }

                System.out.println("查询1997年之后的图书的属性和标题：");

                nodes = (NodeList) xPath.evaluate("//book[@year>1997]/@*|//book[@year>1997]/title/text()",document,XPathConstants.NODESET);

                for (int i = 0; i < nodes.getLength(); i++) {
                    System.out.println(nodes.item(i).getNodeValue());
                }
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
